package com.jason.kotlin.gank.ui

import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.jason.kotlin.gank.R
import com.jason.kotlin.gank.model.RollCommonBean
import com.jason.kotlin.gank.ui.adapter.RollCommonQueryAdapter
import com.jason.kotlin.gank.viewmodel.RollMoreViewModel
import com.jason.library.common.base.BaseStateActivity
import com.jason.library.common.ext.observe

/**
 * @description: RollWorldPhoneCodeActivity
 *
 * @author ChenBaoyang
 * @date 4/24/21 10:13
 */
class RollWorldPhoneCodeActivity : BaseStateActivity<RollMoreViewModel>() {

    private lateinit var refreshLayout: SwipeRefreshLayout
    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: RollCommonQueryAdapter

    override fun bindLiveData() {
        observe(viewModel!!.dataPhoneCodes) {

            val datas = mutableListOf<RollCommonBean>()
            for (i in it.indices) {
                val commonBean = RollCommonBean("${it[i].zhCn} ( + ${it[i].enUs} + )", it[i].phoneCode)
                datas.add(commonBean)
            }
            adapter.setNewData(datas)
        }
    }

    override fun getPageTarget() = recyclerView


    override fun reload() {
    }

    override fun getLayoutId() = R.layout.common_activity_recycler_view

    override fun initView() {
        title = "世界电话区号"
        refreshLayout = findViewById(R.id.swipeRefreshLayout)
        refreshLayout.isRefreshing = false
        refreshLayout.isEnabled =false
        recyclerView = findViewById(R.id.recyclerView)
        val linearLayoutManager =
            LinearLayoutManager(
                this,
                LinearLayoutManager.VERTICAL,
                false
            )
        recyclerView.layoutManager = linearLayoutManager
        recyclerView.itemAnimator = DefaultItemAnimator()

        adapter = RollCommonQueryAdapter(R.layout.roll_item_common_query2)
        recyclerView.adapter = adapter
    }

    override fun onDataInit() {
        viewModel!!.getWorldPhoneCode()
    }
}